package com.bed.lovewithpatientbed.controller;

import com.bed.lovewithpatientbed.core.annocation.LogAnno;
import com.bed.lovewithpatientbed.pojo.WorkerMessage;
import com.bed.lovewithpatientbed.service.IWorkMessageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * @author Tyl
 * @version 1.0.0
 * @ClassName EmailController.java
 * @Description TODO
 * @createTime 2020年10月22日 18:53:00
 */
@Controller
@RequestMapping("email")
public class EmailController {
    @Resource
    IWorkMessageService workMessageService;
    /**
     * @description 重置密码并发送邮箱通知
     * @author tyl
     * @updateTime 2020/10/22 18:56
     * @param
     */
    @RequestMapping("resetPassWord")
    @ResponseBody
    @LogAnno(operateType = "重置密码")
    public String resetPassWord() throws IOException, MessagingException {
        /*读取配置文件*/
        Properties properties = new Properties();
        properties.load(this.getClass().getResourceAsStream("/emailConfig.properties"));
        Session session = Session.getInstance(properties);
        /*创建邮件对象*/
        Message message = new MimeMessage(session);
        try {
            message.setSubject("重置密码");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        /*获得登录对象*/
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpSession httpSession=attr.getRequest().getSession(true);
        WorkerMessage workerMessage = (WorkerMessage) httpSession.getAttribute("workerMessage");
        System.out.println("++++++++++++++"+workerMessage);
        /*重置密码*/
        workMessageService.resetPassword(workerMessage);
        /*填写邮件内容*/
        String msgContent = "亲爱的" + workerMessage.getWorkMessagePhone() + "您好: <br/><br/>"
                          + "您在&nbsp;" +  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "&nbsp;提交重置密码的请求<br/><br/>"
                          + "重置后的密码为：000000"+"&nbsp;&nbsp;请您及时修改密码并妥善保管。<br/><br/>"
                          + "此邮件为系统邮件,请勿回复！";
        /*设置邮件内容为html格式*/
        message.setContent(msgContent,"text/html;charset=utf-8");
        /*设置发件人*/
        message.setFrom(new InternetAddress(MimeUtility.encodeText("重置密码")+ " <"+ properties.getProperty("mail.username")+">"));
        Transport transport = session.getTransport();
        /*连接邮件服务器*/
        transport.connect(properties.getProperty("mail.username"),properties.getProperty("mail.password"));
        /*发送邮件*/
        transport.sendMessage(message,new Address[]{new InternetAddress("tylinemail@163.com")});
        /*关闭连接*/
        transport.close();
        return "success";
    }
}
